import Taro, { Component } from '@tarojs/taro'
import { View, Text, Swiper, SwiperItem, Map, Image } from '@tarojs/components'
import './index.scss'
import { AtButton, AtFab, AtCard, AtInputNumber, AtImagePicker, AtModal, AtModalHeader, AtModalContent, AtModalAction,AtTextarea } from 'taro-ui'
import AtTabBar_index from '../../components/AtTabBar_index';
// 引入SDK核心类
import requets from '../../service/api';
var QQMapWX = require('../../assets/qqmap-wx-jssdk1.2/qqmap-wx-jssdk');
var yiyuan_icon = require('../../assets/img/yiyuan_icon.png')
var liang = require('../../assets/img/liang.png')
var shouji = require('../../assets/img/shouji.png')
var cheqian = require('../../assets/img/cheqian.png')
var chehou = require('../../assets/img/chehou.png')
// 实例化API核心类
var qqmapsdk = new QQMapWX({
  key: 'D43BZ-P7MKO-N4TWG-SGR4H-SLJIE-YAFTN' // 必填
});
Date.prototype.format = function (fmt) {
  var o = {
    "M+": this.getMonth() + 1,                 //月份 
    "d+": this.getDate(),                    //日 
    "h+": this.getHours(),                   //小时 
    "m+": this.getMinutes(),                 //分 
    "s+": this.getSeconds(),                 //秒 
    "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
    "S": this.getMilliseconds()             //毫秒 
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    }
  }
  return fmt;
}
export default class Index extends Component {
  config = {
    navigationBarTitleText: '收集历史'
  }
  state = {
    auditContent:'',
    isOpened: false,
    tong: 0,
    qianke: 0,
    loading: false,
    hou_cx_files: [],
    hou_kf_files: [],
    qian_kf_files: [],
    qian_cx_files: [],
    hou_cx_files2: [],
    hou_kf_files2: [],
    qian_kf_files2: [],
    qian_cx_files2: [],
    hospitalAddress: '',
    hospitalName: '',
    pages: '',
    hospitalId: '',
    userId: '',
    files: [
    ],
    auditType: '',
    time: '',
    disabled: false,
    qian_cx_filess_img: '',
    qian_kf_filess_img: '',
    hou_cx_filess_img: '',
    hou_kf_filess_img: '',
    createTime: '',
    next: '',
    BtnFlag: false,//按钮开关
    july: false,//拒绝理由
  }

  componentWillMount() { }

  componentDidMount() {

    Taro.getStorage({
      key: 'userId',
      success: (res) => {
        // console.log(res.data)
        this.setState({
          userId: res.data
        })
      }
    })
    // console.log(this.$router.params, 123456)


    if (this.$router.params) {

      if (this.$router.params.auditType == '1') {
        //待审批
        this.setState({
          BtnFlag: true
        })

      }

      if (this.$router.params.auditType == '2') {
        //已同意
        this.setState({
          BtnFlag: false
        })

      }

      if (this.$router.params.auditType == '3') {
        //已拒绝
        this.setState({
          BtnFlag: false,
          july: true
        })

      }

      requets.baseOptions({
        url: `/zjn-web/zjn/collector/record/getCollectorRecord?recordId=${this.$router.params.recordId}`,
        method: 'GET',
        data: {}
      }).then((res) => {
        if (res.data.rc === 0) {
          const { auditContent, createTime, collectorWeight, hospitalAddress, hospitalName, hospitalId, auditType, collectorBucketNum, frontCarriageImg, laterCarriageImg, laterWarehouseImg, frontWarehouseImg } = res.data.ret
          let createTimes = new Date(createTime).format("yyyy-MM-dd hh:mm:ss")
          this.setState({
            hospitalAddress, hospitalName, hospitalId,
            tong: collectorBucketNum,
            qianke: collectorWeight,
            qian_cx_filess_img: frontCarriageImg ? 'https://zjn.cloudhw.cn/zjn-web/zjn/common/filehandle/showImage?filepath=' + frontCarriageImg : '',
            qian_kf_filess_img: frontWarehouseImg ? 'https://zjn.cloudhw.cn/zjn-web/zjn/common/filehandle/showImage?filepath=' + frontWarehouseImg : '',
            hou_cx_filess_img: laterCarriageImg ? 'https://zjn.cloudhw.cn/zjn-web/zjn/common/filehandle/showImage?filepath=' + laterCarriageImg : '',
            hou_kf_filess_img: laterWarehouseImg ? 'https://zjn.cloudhw.cn/zjn-web/zjn/common/filehandle/showImage?filepath=' + laterWarehouseImg : '',
            createTime: createTimes,
            auditType: auditType,
            auditContent,
            pages: this.$router.params.pages
          })
        }
      })







    }
  }

  componentWillUnmount() { }

  componentDidShow() { }

  componentDidHide() { }





  onSubmit = () => {
    this.setState({
      loading: true
    })
    let params = {
      collectorBucketNum: this.state.tong,
      collectorWeight: this.state.qianke,
      hospitalId: this.state.hospitalId,
      frontCarriageImg: this.state.qian_cx_files2.length > 0 ? this.state.qian_cx_files2[0].url : '',
      frontWarehouseImg: this.state.qian_kf_files2.length > 0 ? this.state.qian_kf_files2[0].url : '',
      laterCarriageImg: this.state.hou_cx_files2.length > 0 ? this.state.hou_cx_files2[0].url : '',
      laterWarehouseImg: this.state.hou_kf_files2.length > 0 ? this.state.hou_kf_files2[0].url : '',
      submitSite: '',
      userId: this.state.userId
    }
    requets.baseOptions({
      url: `/zjn-web/zjn/collector/record/insertCollectorRecord`,
      method: 'POST',
      data: params
    }).then((res) => {
      if (res.data.rc === 0) {
        // this.setState({
        //   loading: false
        // })
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1000
        })
        setTimeout(() => {
          if (this.state.next == 'renwu') {
            Taro.redirectTo({
              url: '/pages/renwu/index'
            })
          } else {
            Taro.redirectTo({
              url: '/pages/index/index'
            })
          }

        }, 1000)

      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.err,
          duration: 1000
        })
        this.setState({
          loading: false
        })
      }

    })
  }


  onjujue = () => {
    this.setState({
      isOpened: true
    })
  }

  onquxiao = () => {
    this.setState({
      isOpened: false
    })
  }

  onqueding = () => {
    this.setState({
      isOpened: false,
      loading: true
    })
    let params = {
      auditContent: this.state.auditContent,
      auditType: 3,
      recordId: this.$router.params.recordId
    }


    requets.baseOptions({
      url: `/zjn-web/zjn/collector/record/auditLogging`,
      method: 'POST',
      data: params
    }).then((res) => {
      if (res.data.rc === 0) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1000
        })
        if (this.state.pages == '收集历史') {
          Taro.redirectTo({
            url: '/pages/history_GL/index'
          })
        }else{
          Taro.redirectTo({
            url: '/pages/renwu_GL/index'
          })
        }

      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.err,
          duration: 1000
        })



        this.setState({
          loading: false
        })
      }

    })


  }

  onTongyi = () => {
    this.setState({
      loading: true
    })
    let params = {
      auditContent: '',
      auditType: 2,
      recordId: this.$router.params.recordId
    }

    requets.baseOptions({
      url: `/zjn-web/zjn/collector/record/auditLogging`,
      method: 'POST',
      data: params
    }).then((res) => {
      if (res.data.rc === 0) {
        wx.showToast({
          title: '提交成功',
          icon: 'success',
          duration: 1000
        })
        if (this.state.pages == '收集历史') {
          Taro.redirectTo({
            url: '/pages/history_GL/index'
          })
        }else{
          Taro.redirectTo({
            url: '/pages/renwu_GL/index'
          })
        }
      } else {
        wx.showToast({
          icon: 'none',
          title: res.data.err,
          duration: 1000
        })



        this.setState({
          loading: false
        })
      }

    })
  }

  handleChange (event) {
    this.setState({
      auditContent: event.target.value
    })
  }


  render() {
    const { pages, disabled, hou_cx_files, hou_kf_files, tong, hospitalName, hospitalAddress, qianke, qian_kf_files, qian_cx_files, loading, auditType, auditContent, isOpened } = this.state

    return (
      <View className='index'>
        <AtModal isOpened={isOpened}>
          <AtModalHeader>拒绝理由</AtModalHeader>
          <AtModalContent>
            {
              isOpened?            <AtTextarea
              value={this.state.auditContent}
              onChange={this.handleChange.bind(this)}
              maxLength={200}
              placeholder='你的拒绝理由...'
            />:''
            }

          </AtModalContent>
          <AtModalAction> <Button onClick={this.onquxiao}>取消</Button> <Button onClick={this.onqueding}>确定</Button> </AtModalAction>
        </AtModal>
        <AtCard
          isFull={true}
          note={hospitalAddress}
          // extra='额外信息'
          title='收集对象'
          thumb={shouji}
        >
          {hospitalName}


          <View>
            {createTime}
            {
              auditType == 3 ? <View>拒绝理由 : {auditContent}</View> : ''
            }

          </View>

        </AtCard>

        <AtCard
          isFull={true}
          title='医废收集量'
          thumb={liang}
        >
          <View className="AtInputNumber">


            <View>
              桶数:<AtInputNumber
                disabled={true}
                min={0}
                max={999}
                step={1}
                value={tong}
                // onChange={this.handleChange_AtInputNumber.bind(this, 'tong')}
                className="tongshu"
              />
              <AtInputNumber
                disabled={true}
                min={0}
                max={999}
                step={0.1}
                value={qianke}
              // onChange={this.handleChange_AtInputNumber.bind(this, 'qianke')}
              /> 千克
            </View>
          </View>
        </AtCard>

        <AtCard
          isFull={true}
          title='装车前照片'
          thumb={cheqian}
        >
          <View className="AtInputNumber">


            <View className="AtInputNumber">
              库房:
               <Image src={this.state.qian_kf_filess_img} className="AtImagePicker" />


            </View>

            <View className="AtInputNumber">
              车厢:
             <Image src={this.state.qian_cx_filess_img} className="AtImagePicker" />
            </View>
          </View>
        </AtCard>

        <AtCard
          isFull={true}
          title='装车后照片'
          thumb={chehou}
        >
          <View className="AtInputNumber">


            <View className="AtInputNumber">
              库房:
              <Image src={this.state.hou_kf_filess_img} className="AtImagePicker" />
            </View>

            <View className="AtInputNumber">
              车厢:
              <Image src={this.state.hou_cx_filess_img} className="AtImagePicker" />
            </View>
          </View>
        </AtCard>

        {
          this.state.BtnFlag && <View className='BottomBtn'>
            <AtButton loading={loading} type='primary' size="small" className='tijiao' onClick={this.onTongyi}>同意</AtButton>
            <AtButton loading={loading} type='' size="small" className='tijiao' onClick={this.onjujue}>拒绝</AtButton>
          </View>

        }

        <View style={{ height: '20px' }}></View>
      </View>
    )
  }
}
